package com.haiyi.software.app.basic;

import java.io.UnsupportedEncodingException;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.Header;

import android.location.Location;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.haiyi.software.app.MainApplication;
import com.haiyi.software.entity.UserInfo;
import com.haiyi.software.tools.L;
import com.haiyi.software.tools.MyTools;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

public abstract class ActivityPersonalBasic extends ActivityBasic {

	private final static String TAG = "ActivityPersonalBasic";

	private final String str_requestNamegetPerson = "getPerson.php";

	private Boolean isWork = false;

	public Boolean getIsWork() {

		return isWork;
	}

	protected Map<String, Object> mapUser = null;

	protected synchronized void getPersonInfo(String uid) {

		UserInfo myInfo = MainApplication.getInstance().getMineUserInfo();

		if (myInfo != null) {

			Map<String, Object> validator = new HashMap<String, Object>();

			validator.put("uid", myInfo.getUid());
			validator.put("uname", myInfo.getUname());
			validator.put("pwd", myInfo.getPwd());
			validator.put("uguid", myInfo.getUguid());

			Map<String, Object> body = new HashMap<String, Object>();
			body.put("uid", uid);

			Location lc = MyTools.getInstance().getLocation();

			double lat = 0;
			double log = 0;

			if (lc != null) {

				L.i("lc=" + lc.toString());
				lat = lc.getLatitude();
				log = lc.getLongitude();
			}

			body.put("lat", lat);
			body.put("longitude", log);

			Map<String, Object> args = new HashMap<String, Object>();
			args.put("validator", validator);
			args.put("body", body);

			Gson gson = new Gson();
			String s = gson.toJson(args);

			requestNetworkData(s);
		}
	}

	@Override
	protected synchronized void requestNetworkData(Object... args) {

		// TODO 自动生成的方法存根
		if (!isWork) {

			isWork = true;

			String str_requestURL = str_server + str_requestNamegetPerson;

			AsyncHttpClient client = MainApplication.getInstance()
					.getAsyncHttpClient();

			RequestParams params = new RequestParams();

			params.add("args", args[0].toString());

			client.post(str_requestURL, params, responseHandler);

		}
	}

	private AsyncHttpResponseHandler responseHandler = new AsyncHttpResponseHandler() {

		@Override
		public void onSuccess(int statusCode, Header[] headers,
				byte[] responseBody) {

			isWork = false;

			try {

				String str_jsonReturn = new String(responseBody, "UTF-8");

				L.i("person=" + str_jsonReturn);

				Gson gson = new Gson();

				JsonParser parser = new JsonParser();

				JsonObject jsonResult = parser.parse(str_jsonReturn)
						.getAsJsonObject();

				if (jsonResult.get("state").getAsString().equals("200")) {

					Type type = new TypeToken<Map<String, Object>>() {
					}.getType();

					mapUser = gson.fromJson(jsonResult.get("body"), type);

					mapUser.put("spcontent", jsonResult.get("spcontent")
							.getAsString());

					mapUser.put("uhead", jsonResult.get("uhead")
							.getAsJsonArray());

					mapUser.put("uCovers", jsonResult.getAsJsonArray("uCover"));

					L.i(TAG, "mapUser:" + mapUser.toString());

					requestFinish(null);
				}

			} catch (UnsupportedEncodingException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
		}

		@Override
		public void onFailure(int statusCode, Header[] headers,
				byte[] responseBody, Throwable error) {

			isWork = false;

			requestError();
		}
	};

	// private class ThreadNetWorkTask extends
	// AsyncTask<String, String, List<Map<String, Object>>> {
	//
	// @Override
	// protected List<Map<String, Object>> doInBackground(String... params) {
	//
	// // httpPost连接对象
	// HttpPost httpPost = new HttpPost(str_requestURL);
	// // 使用NameValuePair来保存要传递的post阐述
	// List<NameValuePair> params1 = new ArrayList<NameValuePair>();
	// // 添加要传递的参数
	// params1.add(new BasicNameValuePair("args", getJsonArgs()));
	// try {
	// // 设置字符集
	// HttpEntity httpEntity = new UrlEncodedFormEntity(params1,
	// "utf-8");
	// httpPost.setEntity(httpEntity);
	// // 取得默认的HttpClient
	// HttpClient httpClient = new DefaultHttpClient();
	// // 取得HttpResponse
	// HttpResponse httpResponse = httpClient.execute(httpPost);
	// // HttpStatus.SC_OK)表示连接成功
	// if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
	// // 取得返回的字符串
	// String result = EntityUtils.toString(
	// httpResponse.getEntity(), "UTF-8");
	// Log.i(TAG, "result:" + result);
	//
	// Gson gson = new Gson();
	//
	// JsonParser parser = new JsonParser();
	//
	// JsonObject jsonResult = parser.parse(result)
	// .getAsJsonObject();
	//
	// if (jsonResult.get("state").getAsString().equals("200")) {
	//
	// // Log.i(TAG,
	// // jsonResult.get("spcontent").getAsString());
	//
	// Type type = new TypeToken<Map<String, Object>>() {
	// }.getType();
	//
	// mapUser = gson.fromJson(jsonResult.get("body"), type);
	//
	// mapUser.put("spcontent", jsonResult.get("spcontent")
	// .getAsString());
	//
	// mapUser.put("uhead", jsonResult.get("uhead")
	// .getAsJsonArray());
	//
	// Log.i(TAG, "mapUser:" + mapUser.toString());
	// }
	//
	// } else {
	//
	// }
	// } catch (ClientProtocolException e) {
	// Log.e("PostActivity", "ClientProtocolException");
	// e.printStackTrace();
	// } catch (IOException e) {
	// Log.e("PostActivity", "IOException");
	// e.printStackTrace();
	// }
	// return null;
	// }
	//
	// @Override
	// protected void onPostExecute(List<Map<String, Object>> result) {
	//
	// super.onPostExecute(result);
	// isWork = false;
	// requestFinish();
	// }
	// }

	// private String getJsonArgs() {
	//
	// UserInfo myInfo = MainApplication.getInstance().getMineUserInfo();
	//
	// if (myInfo != null) {
	//
	// Map<String, Object> validator = new HashMap<String, Object>();
	//
	// validator.put("uid", myInfo.getUid());
	// validator.put("uname", myInfo.getUname());
	// validator.put("pwd", myInfo.getPwd());
	// validator.put("uguid", myInfo.getUguid());
	//
	// Map<String, Object> body = new HashMap<String, Object>();
	// body.put("uid", strUID);
	//
	// Map<String, Object> args = new HashMap<String, Object>();
	// args.put("validator", validator);
	// args.put("body", body);
	//
	// Gson gson = new Gson();
	// String s = gson.toJson(args);
	//
	// Log.i("argsJson", s);
	//
	// return s;
	// } else {
	// return null;
	// }
	//
	// }

}
